<?php
header("Content-Type: application/json; charset=UTF-8");
require_once 'db.php';

// 检查是否有文件上传
if (!isset($_FILES['image'])) {
    echo json_encode([
        'status' => 'error',
        'message' => '未上传图片'
    ]);
    exit;
}

$image = $_FILES['image'];

// 验证文件类型
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/jpeg'];
if (!in_array($image['type'], $allowedTypes)) {
    echo json_encode([
        'status' => 'error',
        'message' => '只支持 JPG、PNG、GIF 格式的图片'
    ]);
    exit;
}

// 验证文件大小（最大 5MB）
if ($image['size'] > 5 * 1024 * 1024) {
    echo json_encode([
        'status' => 'error',
        'message' => '图片大小不能超过 5MB'
    ]);
    exit;
}

// 创建上传目录
$uploadDir = 'uploads/feedbacks/';
if (!is_dir($uploadDir)) {
    mkdir($uploadDir, 0777, true);
}

// 生成新的文件名
$fileExtension = pathinfo($image['name'], PATHINFO_EXTENSION);
$newFileName = date('YmdHis') . '_' . uniqid() . '.' . $fileExtension;
$imagePath = $uploadDir . $newFileName;

// 移动文件到目标目录
if (move_uploaded_file($image['tmp_name'], $imagePath)) {
    echo json_encode([
        'status' => 'success',
        'message' => '图片上传成功',
        'image_url' => $imagePath  // 返回相对路径，前端会拼接 backendUrl
    ]);
} else {
    echo json_encode([
        'status' => 'error',
        'message' => '图片上传失败'
    ]);
}
?> 